﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataTransfer;

namespace BUS
{
  public  class UserBus
    {
        public string connect = QuiDinhBus.connect;
        public static QLPMDataContext db;
        // db = new QLPMDataContext(connect);
        public IEnumerable<User> GetAll()
        {
            IEnumerable<User> user = null;
            try
            {
                db = new QLPMDataContext(connect);
               user = from t in db.Users select t;
                //db.Dispose();

            }
            catch (Exception)
            {

                throw;
            }
            return user;


        }
        public User Get(string key)
        {
            User info = new User();
            info = null;
            try
            {
                db = new QLPMDataContext(connect);
                info = (from qd in db.Users where qd.UserName == key select qd).SingleOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return info;
        }
        public bool Insert(User info)
        {
            bool flag = false;
            try
            {
                 db = new QLPMDataContext(connect);
                db.Users.InsertOnSubmit(info);
                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }

            return flag;
        }
        public bool Delele(string key)
        {
            bool flag = false;
            try
            {

                db = new QLPMDataContext(connect);

                User del = (from t in db.Users where t.UserName == key select t).SingleOrDefault();

                db.Users.DeleteOnSubmit(del);

                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public bool Update(User info)
        {
            bool flag = false;
            try
            {
                db = new QLPMDataContext(connect);
                var t = (from user in db.Users where user.UserName == info.UserName select user).SingleOrDefault();
                t.UserName = info.UserName;
                t.Pass = info.Pass;
                t.HoTen = info.HoTen;
                t.NgaySinh = info.NgaySinh;
                t.GioiTinh = info.GioiTinh;
                t.act = info.act;
               
                db.SubmitChanges();


                flag = true;

            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
    }
}
